'use strict';
app.controller('SysUserCtrl', ['$scope', '$modal', '$rootScope', '$state', '$filter', 'toaster', 'commonService', 'userService', function ($scope, $modal, $rootScope, $state, $filter, toaster, commonService, userService) {
    $scope.page_code = "sysUser";
    $scope.matchString = "";
    function getTbDefinitionsFromServer() {
        return {
            "serverSide": true,
            "ajax": {
                url: "sysuser/plist", data: function (d) {
                    d.matchString = $scope.matchString;
                    for (var key in d) {
                        if (key.indexOf("columns") == 0) {
                            delete d[key];
                        }
                    }
                }
            },
            aoColumns: [{
                mData: 'id', sClass: "center"
            }, {mData: 'name'}, {mData: 'roleName'}, {mData: 'status'}, {mData: 'orderId'}, {mData: 'account'}, {mData: 'createDate'}, {mData: 'updateDate'}, {mData: 'note'}],
            aaSorting: [[4, "desc"]],
            aoColumnDefs: [{
                aTargets: [0], mRender: function (data, type, full) {
                    var editId = "sysUser" + data;
                    return '<label class="i-checks m-b-none"><input id = "' + editId + '" class="selectListCmPara" type="checkbox"><i></i></label>';
                }, bSortable: false

            },commonService.tableRowAllowNullNoSort(1),commonService.tableRowAllowNullNoSort(2)
                , {
                    aTargets : [ 3 ],
                    mRender : function(data, type,
                                       full) {
                        if(data == 0){
                            return "待审核";
                        }else if(data == 1){
                            return "启用";
                        } else if(data==2){
                            return "禁用"
                        }
                        return "";
                    }
                },commonService.tableRowAllowNullNoSort(4)
                ,commonService.tableRowAllowNullNoSort(5),{
                    aTargets : [ 6 ],
                    mRender : function(data, type,

                                       full) {
                        if (data) {
                            var date = $filter('date')(new Date(data), 'yyyy-MM-dd HH:mm');
                            return date;
                        }
                        return "";
                    },
                    bSortable: false
                }
                , {
                    aTargets: [7],
                    mRender: function (data, type,
                                       full) {
                        if (data) {
                            var date = $filter('date')(new Date(data), 'yyyy-MM-dd HH:mm');
                            return date;
                        }
                        return "";
                    },
                    bSortable: false
                }, commonService.tableRowAllowNullNoSort(8)
            ],
            fnDrawCallback: function (oSettings, json) {
                $(".selectListCmPara").on('click', function () {
                    $(".selectListCmPara").not(this).attr("checked", false);
                });
            }
        };
    };
    $scope.tbOptions = getTbDefinitionsFromServer();
    $scope.add = function () {
        var param = {type: 'create'};
        commonService.openEditDialog(param, 'edit', 'SysUserEditCtrl');
    };
    $scope.edit = function () {
        var info = commonService.getSelectData("sysUserTable", "sysUser", "id");
        if (info != null) {
            var param = {type: 'edit', info: info};
            commonService.openEditDialog(param, 'edit', 'SysUserEditCtrl');
        } else {
            toaster.pop('info', '提示', '请选择需要修改的信息。');
        }
    };
    $scope.delete = function () {
        var info = commonService.getSelectData("sysUserTable", "sysUser", "id");
        if (info != null) {
            BootstrapDialog.confirm("确认要删除：" + info.name, function (result) {
                if (result) {
                    commonService.httpJson("sysuser/ditem", {id: info.id}).success(function (data, status) {
                        if (data && data['code'] == 0) {
                            toaster.pop('info', '提示', data['message']);
                            $scope.refresh();
                        } else {
                            commonService.toasterError(data['message']);
                        }
                    });
                }
            });
        } else {
            toaster.pop('info', '提示', '请选择需要修改的信息。');
        }
    };
    $scope.refresh = function () {
        var table = $('#sysUserTable').DataTable();
        table.ajax.reload();
    };
    $scope.search = function () {
        $scope.refresh();
    };
    $scope.$on('sysuser.update', function (event) {
        $scope.refresh();
    });
}]);
app.controller('SysUserEditCtrl', ['$scope', '$rootScope', '$modalInstance', 'param', 'toaster', 'commonService', 'userService', function ($scope, $rootScope, $modalInstance, param, toaster, commonService, userService) {
    $scope.sysUser = {};
    $scope.roles = [];
    getRoles();
    function getRoles() {
        commonService.httpJson("sysrole/drop", {}).success(function (data) {
            if (data && data['code'] == 0) {
                $scope.roles = data['data'];
            } else {
                commonService.toasterError(data['message']);
            }
        });
    }

    if (param.type == "edit") {
        getInfo();
    }
    function getInfo() {
        commonService.httpJson("sysuser/item", {id: param.info.id}).success(function (data) {
            if (data && data['code'] == 0) {
                $scope.sysUser = data['data'];
            } else {
                commonService.toasterError(data['message']);
            }
        });
    };

    function checkSave() {
        var result = true;

        if (!$scope.sysUser.name) {
            commonService.toasterError("名称不能为空");
            result = false;
        }

        if (!$scope.sysUser.account) {
            commonService.toasterError("帐号不能为空");
            result = false;
        }

        return result;
    }

    $scope.submit = function () {
        if (!checkSave()) {
            return;
        }

        var salt = "1a2b3c4d5e6f7g8h9j0k";
        $scope.sysUser.password = hex_md5(salt.charAt(0) + salt.charAt(2) + $scope.sysUser.password + salt.charAt(5) + salt.charAt(9));
        if (param.type == "edit") {
            $scope.sysUser.updateUser = userService.getId();
        } else {
            $scope.sysUser.createUser = userService.getId();
        }
        commonService.httpJson("sysuser/save", $scope.sysUser).success(function (data) {

            if (data && data['code'] == 0) {
                commonService.editData("sysuser.update", param.type);
                commonService.toasterSuccess(data['message']);
                $modalInstance.close();
            } else {
                $scope.sysUser.password = "";
                commonService.toasterError(data['message']);
            }
        });
    }
    $scope.cancel = function () {
        $modalInstance.dismiss('cancel');
    };
}]);